package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.result.R;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /*
    * 菜品新增
    * */
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.addTwo(dishDto);

        return R.success("添加成功！");
    }

    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
       // Page<DishDto> pageDto = new Page<>();

       /* LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Dish::getName, name)
                .orderByDesc(Dish::getUpdateTime);

        dishService.page(pageDish, lqw);*/
        R<Page> pageR = dishService.selevtAll(page, pageSize, name);
        return pageR;
    }

    /*
    * 根据id查询菜品与口味信息
    * */
    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable Long id) {
        DishDto dishDto = dishService.selectTwo(id);

        return R.success(dishDto);
    }

    /*
     * 修改菜品与口味信息
     * */
    @PutMapping
    public R<String> updateTwo(@RequestBody DishDto dishDto) {
        dishService.updateTwo(dishDto);

        return R.success("修改成功！");
    }

    /*
     * 修改菜品售卖状态
     * */
    @PostMapping("/status")
    public R<String> update(@RequestBody Dish dish) {
        dishService.updateById(dish);

        return R.success("状态修改成功！");
    }

    /*
     * 删除菜品
     * */
    @DeleteMapping("/{ids}")
    public R<String> delete(@PathVariable Long[] ids) {
        dishService.removeById(ids);
        return R.success("删除成功！");
    }

    /*
     * 根据菜品种类显示菜品
     * */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId())
                .eq(Dish::getStatus, 1)
                .orderByAsc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(lqw);
        //新建DishDto类型的集合用于封装结果返回前端
        List<DishDto> dishDtoList = new ArrayList<>();
        //遍历之间的dish类型的集合数据赋值给DishDto
        for (Dish dishs : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dishs,dishDto);
            //获取dish的id用于条件查询
            Long dishId = dishs.getId();
            //添加口味信息
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

            dishDto.setFlavors(flavors);
            dishDtoList.add(dishDto);
        }

        return R.success(dishDtoList);
    }

    /*
     * 菜品售卖状态修改
     * */
    @PostMapping("/status/{status}")
    public R<String> dishStatus(@PathVariable Integer status, Long[] ids) {
        dishService.dishStatus(status,ids);

        return R.success("售卖状态修改成功！");
    }

    /*
     * 删除菜品与口味信息
     * */
    @DeleteMapping
    public R<String> deleteDish(@RequestParam List<Long> ids) {
        dishService.dishDelete(ids);

        return R.success("删除成功！");
    }
}
